package com.cls.business.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cls.common.annotation.ControllerEndpoint;
import com.cls.common.utils.MyUtil;
import com.cls.common.entity.MyConstant;
import com.cls.common.controller.BaseController;
import com.cls.common.entity.QueryRequest;
import com.cls.common.entity.ResponseVO;
import com.github.pagehelper.PageInfo;
import com.cls.business.entity.MemberResult;
import com.cls.business.service.IMemberResultService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 课题组成员成果信息表 Controller
 *
 * @author wandering
 * @date 2020-11-12 15:28:55
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class MemberResultController extends BaseController {

    private final IMemberResultService memberResultService;

    /**
    * 课题组成员成果信息表列表页面
    **/
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/memberResult")
    public String memberResultIndex(){
        return MyUtil.view("business/memberResult/memberResult");
    }

    /**
    * 课题组成员成果信息表新增页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/memberResult/memberResultAdd")
    public String memberResultAdd(Model model){
        return MyUtil.view("business/memberResult/memberResultAdd");
    }

    /**
    * 课题组成员成果信息表修改页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/memberResult/memberResultUpdate/{memberResultId}")
    public String memberResultUpdate(@PathVariable Long memberResultId, Model model){
        MemberResult memberResult = memberResultService.getById(memberResultId);
        model.addAttribute("memberResult",memberResult);
        return MyUtil.view("business/memberResult/memberResultUpdate");
    }



    /**
    * 课题组成员成果信息表详情页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/memberResult/memberResultDetail/{memberResultId}")
    public String memberResultDetail(@PathVariable Long memberResultId, Model model){
        MemberResult memberResult = memberResultService.getById(memberResultId);
        model.addAttribute("memberResult",memberResult);
        return MyUtil.view("business/memberResult/memberResultDetail");
    }

    @GetMapping("memberResult")
    @ResponseBody
    @RequiresPermissions("memberResult:list")
    public ResponseVO getAllMemberResults(MemberResult memberResult) {
        return ResponseVO.success(memberResultService.findMemberResults(memberResult));
    }

    @GetMapping("{declareId}/memberResult/list")
    @ResponseBody
    @RequiresPermissions("memberResult:list")
    public ResponseVO memberResultList(QueryRequest request, @PathVariable long declareId) {
        MemberResult memberResult = new MemberResult();
        memberResult.setDeclareId(declareId);
        PageInfo<MemberResult> pageInfo =  this.memberResultService.findMemberResults(request, memberResult);
        return ResponseVO.success(pageInfo);
    }

    @ControllerEndpoint(operation = "新增MemberResult", exceptionMessage = "新增MemberResult失败")
    @PostMapping("memberResult")
    @ResponseBody
    @RequiresPermissions("memberResult:add")
    public ResponseVO addMemberResult(@Valid MemberResult memberResult) {
        this.memberResultService.createMemberResult(memberResult);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "多个删除MemberResult", exceptionMessage = "删除MemberResult失败")
    @PostMapping("memberResult/deletes/{ids}")
    @ResponseBody
    @RequiresPermissions("memberResult:delete")
    public ResponseVO deleteMemberResults(@PathVariable String ids) {
        String[] idArry = ids.split(StringPool.COMMA);
        this.memberResultService.deleteMemberResults(idArry);
        return ResponseVO.success();
    }


    @ControllerEndpoint(operation = "删除单个MemberResult", exceptionMessage = "删除MemberResult失败")
    @PostMapping("memberResult/delete/{memberResultId}")
    @ResponseBody
    @RequiresPermissions("memberResult:delete")
    public ResponseVO deleteMemberResult(@PathVariable Long memberResultId) {
        this.memberResultService.removeById(memberResultId);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改MemberResult", exceptionMessage = "修改MemberResult失败")
    @PostMapping("memberResult/update")
    @ResponseBody
    @RequiresPermissions("memberResult:update")
    public ResponseVO updateMemberResult(MemberResult memberResult) {
        this.memberResultService.updateMemberResult(memberResult);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改MemberResult", exceptionMessage = "导出Excel失败")
    @PostMapping("memberResult/excel")
    @ResponseBody
    @RequiresPermissions("memberResult:export")
    public void export(QueryRequest queryRequest, MemberResult memberResult, HttpServletResponse response) {
        List<MemberResult> memberResults = this.memberResultService.findMemberResults(queryRequest, memberResult).getList();
        ExcelKit.$Export(MemberResult.class, response).downXlsx(memberResults, false);
    }
}
